package top.lingkang.finalgateway.admin.config;

import cn.hutool.core.io.IoUtil;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import top.lingkang.finalgateway.FinalGateway;
import top.lingkang.finalgateway.utils.IdUtils;
import top.lingkang.mm.gen.IdGenerate;
import top.lingkang.mm.orm.MapperManage;
import top.lingkang.mm.solon.SolonMagicConfiguration;
import top.lingkang.mm.solon.SolonMapperManage;
import top.lingkang.mm.utils.MagicUtils;

import javax.sql.DataSource;
import java.io.File;
import java.sql.SQLException;

/**
 * @author lingkang
 * Created by 2024/4/28
 */
@Configuration
@Slf4j
public class AdminConfig {
    private final File dbFile;
    private boolean needInit = true;


    public AdminConfig() {
        dbFile = new File(FinalGateway.dir + File.separator + "conf" + File.separator + "db_v1.0.0");
        if (new File(dbFile.getAbsolutePath() + ".mv.db").exists())
            needInit = false;
    }

    @Bean
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName("org.h2.Driver");
        // config.setJdbcUrl("jdbc:h2:" + dbFile.getAbsolutePath());
        config.setJdbcUrl("jdbc:h2:file:./conf/db_v1.0.0");
        config.setUsername("root");
        config.setPassword("123456");
        config.setMaximumPoolSize(20);
        config.setMaxLifetime(600000);
        HikariDataSource dataSource = new HikariDataSource(config);
        return dataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(@Inject DataSource dataSource) {
        SolonMagicConfiguration configuration = new SolonMagicConfiguration(dataSource);
        // 推荐开启
        configuration.setMapUnderscoreToCamelCase(true);// 下划线转驼峰

        // 文件加载 resources/mapper/UserMapper.xml
        configuration.addMapperXml("mapper");
        // 直接者添加 mapper 接口，但此时无法扫描到xml
        // configuration.addMapper(UserMapper.class);

        SqlSessionFactory sqlSessionFactory = configuration.build();
        if (needInit) {
            try {
                log.info("初始化sql脚本");
                MagicUtils.exeScript(
                        IoUtil.readUtf8(getClass().getClassLoader().getResourceAsStream("sql/v1.0.0.sql")),
                        dataSource.getConnection()
                );
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        // 自定义id生成
        configuration.setIdGenerate(new IdGenerate() {
            @Override
            public Object nextId(String param) {
                return IdUtils.nextIdStr();
            }
        });
        return sqlSessionFactory;
    }

    @Bean
    public MapperManage mapperManage(@Inject SqlSessionFactory sqlSessionFactory) {
        SolonMapperManage manage = new SolonMapperManage(
                sqlSessionFactory.getConfiguration(),
                sqlSessionFactory.openSession());
        return manage;
    }
}
